// 朋友圈
const express=require('express')
const pool=require('../pool')
const router=express.Router()

// 发朋友圈
router.post('/add',(req,res,next)=>{
	let obj=req.body
	pool.query('insert into friend set ?',[obj],(err,r)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'发朋友圈成功',data:r})
	})
})

// 查询所有
router.get('/queryAll',(req,res,next)=>{
	pool.query('select * from friend',(err,r)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'查找所有动态成功',data:r})
	})
})

// 模糊查询搜索
router.get('/queryLike',(req,res,next)=>{
	let obj=req.query
	let name1=obj.userName
	pool.query('select * from friend where userName like "%?%"',[name1],(err,r)=>{
		if(err){
			return next(err)
		}
		if(r.length){
			res.send({code:200,msg:'搜索成功',data:r})
		}else{
			res.send({code:404,msg:'查无此动态',data:r})
		}
		
	})
})

// 删除
router.get('/delete',(req,res,next)=>{
	let obj=req.query
	
	pool.query('delete from friend where friendId=?',[obj.friendId],(err,r)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'删除动态成功',data:r})
	})
})

// 修改
router.post('/modify',(req,res,next)=>{
	let obj=req.body
	pool.query('update friend set ? where friendId=?',[obj,obj.friendId],(err,r)=>{
		if(err){
			return next(err)
		}
		res.send({code:200,msg:'修改动态成功',data:r})
	})
})

module.exports=router